@echo off

pushd "%~dp0%"

IF "%1" == "/db-new" goto again
IF "%1" == "/zap" goto again
IF "%1" == "/drop-db" goto again
IF "%1" == "/create-db" goto again
IF "%1" == "/all" goto again
IF "%1" == "/clean" goto again
IF "%1" == "/clean-views" goto again
if "%1" == "/clean-tables" goto again
if "%1" == "/clean-sps" goto again

if "%1" == "/table-p1" goto again
if "%1" == "/table-p2" goto again
if "%1" == "/relation" goto again
if "%1" == "/rel-sp-add" goto again
if "%1" == "/rel-sp-update" goto again
if "%1" == "/rel-sp-delete" goto again
if "%1" == "/rel-sp-count" goto again
if "%1" == "/view-count" goto again
if "%1" == "/view-base" goto again
if "%1" == "/sp-add" goto again
if "%1" == "/sp-delete" goto again
if "%1" == "/sp-update" goto again
if "%1" == "/sp-count"  goto again
if "%1" == "/sp-search" goto again
if "%1" == "/sp-get" goto again
if "%1" == "/sp-getbyid" goto again

if "%1" == "/pause"  goto again

SET DBNAME=%1
shift

SET DBSERVER=%1
shift

echo Database: %DBNAME%
echo Server: %DBSERVER%

:again
if "%1" == "/zap" call sqlrun /drop-db /create-db /all
if "%1" == "/db-new" call sqlrun /create-db /all
if "%1" == "/all" call sqlrun /table-p1 /table-p2 /relation /view-count /view-base /rel-sp-add /rel-sp-delete /rel-sp-update /rel-sp-count /sp-add /sp-delete /sp-update /sp-count /sp-search /sp-get /sp-getbyid

if "%1" == "/create-db" osql -E -S %DBSERVER% -d master -Q "CREATE DATABASE [%DBNAME%]"
if "%1" == "/drop-db" (
	osql -E -S %DBSERVER% -d master -Q "ALTER DATABASE [%DBNAME%] SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
	osql -E -S %DBSERVER% -d master -Q "ALTER DATABASE [%DBNAME%] SET SINGLE_USER"
	osql -E -S %DBSERVER% -d master -Q "DROP DATABASE [%DBNAME%]"
)


if "%1" == "/clean" for %%i in (op\*.sql) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/clean-views" for %%i in (op\drop_views.sql) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/clean-tables" for %%i in (op\drop_tables.sql) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/clean-sps" for %%i in (op\drop_storeprocs.sql) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i

if "%1" == "/table-p1" for %%i in (create_table_p1_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/table-p2" for %%i in (create_table_p2_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/relation" for %%i in (create_relation_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/rel-sp-add" for %%i in (create_rel_sp_add_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/rel-sp-update" for %%i in (create_rel_sp_update_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/rel-sp-delete" for %%i in (create_rel_sp_delete_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/rel-sp-count" for %%i in (create_rel_sp_count_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/view-count" for %%i in (create_view_count_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/view-base" for %%i in (create_view_base_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/sp-add" for %%i in (create_sp_add_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/sp-delete" for %%i in (create_sp_delete_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/sp-update" for %%i in (create_sp_update_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/sp-count" for %%i in (create_sp_count_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/sp-search" for %%i in (create_sp_search_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/sp-get" for %%i in (create_sp_get_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i
if "%1" == "/sp-getbyid" for %%i in (create_sp_getbyid_*) do osql -E -S %DBSERVER% -d %DBNAME% -i %%i

if "%1" == "/pause" (
	echo.
	pause
)

shift
if NOT "%1" == "" goto again

popd
